package zq.cn.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

import zq.cn.adapter.MyAdapter;
import zq.cn.utils.BtnTools;
import zq.cn.utils.SqlDao;

public class ViewInfoActivity extends BtnTools {
    private ListView list;
    private TextView tv;
    private MyAdapter adapter;
    private List<Map<String, Object>> values;

    private int pagenum =1;
    private int pagesie =10;
    private int recordCound;
    private int pageCount;
    private String keyword;

    int first;
    int visible;
    int total;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pager_view_info);

        list = (ListView) findViewById(R.id.list);
        tv = (TextView) findViewById(R.id.top_text);
        tv.setText("全部问题");
        super.init(1);

        keyword = getIntent().getStringExtra("keyword");
        if(keyword==null) {
            keyword ="";
        }

//        values = SqlDao.ViewAllData();
        values = SqlDao.ViewPageData(pagenum, pagesie, keyword);
        recordCound = SqlDao.getCound(keyword);
        pageCount = (recordCound-1)/pagesie+1;

        final TextView text = new TextView(this);
        text.setText("正在刷新。。。。");
        text.setTextColor(Color.RED);
        text.setTextSize(14);
        list.addFooterView(text);
        adapter = new MyAdapter(this, values);
//        Log.i("zq",adapter+"----------------------");
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LinearLayout layout = (LinearLayout) view;
//                layout.getChildAt(1).setVisibility(View.VISIBLE);//设置显示属性
                Map<String, Object> map = values.get(position);
                //答案的显示和隐藏
                if (layout.getChildCount() == 1) {
                    //如果得到LinearLayout的TextView总数为1时，加入answer
                    //新建TextView，放入文本
                    TextView answer = new TextView(ViewInfoActivity.this);
                    answer.setTextSize(14);
                    answer.setTextColor(Color.RED);
                    answer.setText(map.get("answer").toString());
                    layout.addView(answer);
                    map.put("flag", true);
                } else {
                    //删除answer
                    layout.removeViewAt(1);
                    map.put("flag", false);
                }
            }
        });

        list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(scrollState==SCROLL_STATE_IDLE && first+visible==total && total!=0) {
                    if(pagenum<=pageCount) {
                        pagenum++;
                        values.addAll(SqlDao.ViewPageData(pagenum, pagesie, keyword));
                        adapter.notifyDataSetChanged();
                    }else {
                        if(list.getFooterViewsCount()>0) {
                            list.removeFooterView(text);
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                first = firstVisibleItem;
                visible = visibleItemCount;
                total = totalItemCount;
            }
        });
    }
}
